﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SystemHelperLibrary
{
	namespace CollectionExt
	{
		using System.Collections;
		using System.Threading;

		public static class IEnumerableHelper
		{
			/// <summary>
			/// Performs a given action on every item of a given sequence.
			/// </summary>
			/// <typeparam name="T">The collection's content.</typeparam>
			/// <param name="source">The sequence to be processed.</param>
			/// <param name="action">An action delegate that is being invoked
			/// for every item of the <paramref name="source"/> sequence.</param>
			/// <exception cref="ArgumentNullException">If <paramref name="action"/>
			/// is a null reference.</exception>
			public static void Do<T>(this IEnumerable<T> source, Action<T> action)
			{
				if (action == null) throw new ArgumentNullException("action");

				foreach (var item in source)
				{
					action(item);
				}
			}

			public static void DoAsync<T>(this IEnumerable<T> source, Action<T> action)
			{
				if (action == null) throw new ArgumentNullException("action");

				foreach (var item in source)
				{
					ThreadPool.QueueUserWorkItem(state => action((T)state), item);
				}
			}
		}

		public static class DictionaryHelper
		{
			private static object syncRoot = new object();

			public static Hashtable ToHashTable<TKey, TValue>(Dictionary<TKey, TValue> dict)
			{
				lock (syncRoot)
				{
					Hashtable table = new Hashtable(dict.Count);

					foreach (KeyValuePair<TKey, TValue> pair in dict)
					{
						table.Add(pair.Key, pair.Value);
					}
					return table;
				}
			}

			/// <summary>
			/// Convert to correct type.
			/// </summary>
			/// <typeparam name="T"></typeparam>
			/// <param name="input"></param>
			/// <returns></returns>
			public static T ConvertTo<T>(object input)
			{
				object result = default(T);
				if (typeof(T) == typeof(int))
					result = System.Convert.ToInt32(input);
				else if (typeof(T) == typeof(long))
					result = System.Convert.ToInt64(input);
				else if (typeof(T) == typeof(string))
					result = System.Convert.ToString(input);
				else if (typeof(T) == typeof(bool))
					result = System.Convert.ToBoolean(input);
				else if (typeof(T) == typeof(double))
					result = System.Convert.ToDouble(input);
				else if (typeof(T) == typeof(DateTime))
					result = System.Convert.ToDateTime(input);
				else
					result = input;

				return (T)result;
			}
		}

		public static class ListExtension
		{
			/// <summary>
			/// AddRange of items of same type to IList 
			/// </summary>
			/// <typeparam name="T"></typeparam>
			/// <param name="items"></param>
			/// <param name="itemsToAdd"></param>
			public static IList<T> AddRange<T>(this IList<T> items, IList<T> itemsToAdd)
			{
				if (items == null || itemsToAdd == null)
					return items;

				foreach (T item in itemsToAdd)
					items.Add(item);

				return items;
			}

		}
	}
}
